import React, { Component } from 'react'; /* eslint-disable eqeqeq */
import PubSub from 'pubsub-js'
import { Regular, } from '../../../const/infoConst';
import { Link, Navigate } from 'react-router-dom';
import { meshLog, SPRemove, SPGet, SPSet } from '../../../config';
import { Spin } from 'antd';
import './bomb.less'
import { profileDataDownload } from '../../../utils/profileUtils';
import axios from 'axios';
import { getversion, getmeshInfo } from "../../../api/index"
class Bomb extends Component {
    constructor(a) {
        super(a);
        this.state = {
            zongkongzhi: '1',
            flag: 0,
            dizhilan: ''
        }
    }
    componentDidMount() {
        // setTimeout(() => {
        //     this.setState({
        //         zongkongzhi: '0',
        //     })
        // }, 10000)
        PubSub.unsubscribe("mqttguanbi")
        PubSub.subscribe('mqttguanbi', (msg, index) => {
            meshLog(' bomb.jsx 28行 mqtt信息结束，显示OK弹框');
            const dizhilanpandun = JSON.parse(SPGet("dizhilan"))
            let dizhilan = ''
            if (dizhilanpandun == 0) {
                dizhilan = '/admin/home'
            } else if (dizhilanpandun == 1) {
                dizhilan = '/admin/devices'
            } else if (dizhilanpandun == 2) {
                dizhilan = '/admin/scenes'
            } else if (dizhilanpandun == 3) {
                dizhilan = '/admin/smartshift'
            } else {
                dizhilan = '/admin/schedules'
            }
            meshLog('bomb.jsx 42行 dizhilan', dizhilanpandun);
            //要显示OK弹框的代码
            this.setState({
                zongkongzhi: '0',
                dizhilan: dizhilan
            })
            //不显示弹框直接点，这边目前没有解决跳转的问题
            // this.closeBtnClick()
            // history.push('/web/admin/home')
            // //history.go(0);//代表刷新页面
            // let shuaxincishu = (SPGet("shuaxincishu"));
            // if (shuaxincishu == 0) {
            //     SPSet('shuaxincishu', JSON.stringify(1));//控制刷新页面的地方
            //     history.go(0);//代表刷新页面
            // } else {
            //     meshLog('不用刷新');
            // }
        })
        PubSub.unsubscribe("mqttguanbi2")
        PubSub.subscribe('mqttguanbi2', (msg, index) => {
            meshLog(' bomb.jsx 28行 mqtt信息结束，显示OK弹框');
            //PubSub.publish('setInterval', '') //这个是mqtt没有收到信息，需要强制把单发清除
            const dizhilanpandun = JSON.parse(SPGet("dizhilan"))
            let dizhilan = ''
            if (dizhilanpandun == 0) {
                dizhilan = '/admin/home'
            } else if (dizhilanpandun == 1) {
                dizhilan = '/admin/devices'
            } else if (dizhilanpandun == 2) {
                dizhilan = '/admin/scenes'
            } else if (dizhilanpandun == 3) {
                dizhilan = '/admin/smartshift'
            } else {
                dizhilan = '/admin/schedules'
            }
            meshLog('bomb.jsx 42行 dizhilan', dizhilanpandun);
            //要显示OK弹框的代码
            this.setState({
                zongkongzhi: '0',
                dizhilan: dizhilan
            })
        })
        //这个是切换站点的
        PubSub.unsubscribe("bomb2")
        PubSub.subscribe('bomb2', (msg, index) => {
            PubSub.publish('setInterval', '') //需要强制把单发清除，清除上一次没有接受完的数据
            this.setState({
                zongkongzhi: '1',
            })
            window.location.reload();
            SPSet('shuaxincishu', JSON.stringify(0));//控制刷新页面的地方
            SPSet('dizhilan', JSON.stringify(0));//控制OK跳转的页面所需要的具体数据
            SPSet('zhandengpanduan', JSON.stringify(0));//控制第一次加载是home界面右边显示的数据
            // setTimeout(() => {
            //     this.setState({
            //         zongkongzhi: '0',
            //     })
            // }, 10000)
        })
    }
    closeBtnClick = () => {
        meshLog("bomb.jsx 77行，closeBtnClick");
        PubSub.publish('guanbi', '')//关闭admin界面弹框
        //PubSub.publish('setInterval', '')//清除自动循环单发的代码
        setTimeout(() => {
            PubSub.publish('changehrefname', '')//修改导航文字高亮
        }, 500)
        SPRemove('isLogin');//删除loaclstorage上的deviceSetx
        SPSet('isLogin', JSON.stringify(false))
        SPRemove('bomb2');//删除loaclstorage上的bomb2所需要的值
        SPSet('bomb2', JSON.stringify(0));
        //30秒自动刷新的 myFunction();
        const email = (SPGet("email"))
        const token = (SPGet("token"))
        const meshUUID = (SPGet("meshUUID"))
        async function myFunction() {
            const version = (SPGet("version"))
            let resultt = await getversion(email, token, meshUUID)
            //获取整个meshinfo的信息
            const meshkong = ''
            let resultmesh = await getmeshInfo(email, token, meshkong)
            let meshListNew = resultmesh.meshList
            meshLog('bomb.jsx 97行 服务器的meshListNew', meshListNew);
            const meshListOld = JSON.parse(SPGet("meshList"))
            let ischangemesh = 0
            if (meshListNew.length == meshListOld.length) {
                for (let i = 0; i < meshListOld.length; i++) {
                    let meshListOldUUID = meshListOld[i].meshUUID
                    let ischange = false
                    for (let j = 0; j < meshListNew.length; j++) {
                        if (meshListOldUUID == meshListNew[j].meshUUID) {
                            ischange = true
                        }
                    }
                    if (!ischange) {
                        ischangemesh = 1
                    }
                }
            } else {
                //修改站点信息，长度不一样
                ischangemesh = 1
            }
            if (ischangemesh == 1) {
                //修改整个meshListNew的布局
                //匹配老的当前站点的uuid，如果老的不在新的站点列表里面，就走切换站点，如果在，只刷新侧边栏的站点列表
                const meshUUIDold = (SPGet("meshUUID"))
                let uuidpanduan = meshListNew.find(item => item.meshUUID == meshUUIDold);
                // meshLog('meshUUIDold', meshUUIDold);
                // meshLog('uuidpanduan', uuidpanduan);
                if (uuidpanduan === undefined) {
                    meshLog('bomb.jsx 126行 这个新的站点列表里面没有原本的站点信息，需要重新获取页面信息', resultmesh);
                    PubSub.publish('changemeshListNew', meshListNew)
                } else {
                    meshLog('bomb.jsx 129行 不需要修改信息，只需要修改站点列表信息');
                    for (let index = 0; index < meshListNew.length; index++) {
                        if (meshListNew[index].meshUUID == meshUUIDold) {
                            meshListNew[index].isSelected = 1
                        } else {
                            meshListNew[index].isSelected = 0
                        }
                    }
                    //meshLog('修改之后的meshListNew', meshListNew);
                    PubSub.publish('changeUIlist', meshListNew)
                }
            }
            else {
                //版本号判断
                meshLog('bomb.jsx 143行wjk local ver:', version, ' server:' + resultt.version);
                if (resultt.version <= version) {
                    meshLog('bomb.jsx 145行 版本一样，不用操作');
                } else {
                    //这是不对的操作
                    // PubSub.publish('showrefresh', '')
                    // SPSet('shuaxincishu', JSON.stringify(0));//控制刷新页面的地方
                    // let result = await getmeshInfo(email, token, meshUUID)
                    // SPRemove('version');//删除loaclstorage上的deviceSetx
                    // const versionx = result.meshList[0].version;
                    // //meshLog("versionx", versionx);//将修改之后的数据赋值给deviceSetx
                    // SPSet('version', JSON.stringify(versionx))//从新上传到loaclstorage
                    // const dizhilanbanben = JSON.parse(SPGet("dizhilan"))
                    // SPSet('dizhilanbanben', JSON.stringify(dizhilanbanben));//控制OK跳转的页面所需要的具体数据,刷新版本需要的
                    // const dUrl = result.meshList[0].profile;
                    // axios.get(dUrl)
                    //     .then(async (value) => {
                    //         //输出加密的json meshLog(value.data)
                    //         profileDataDownload(value);
                    //     })
                    //     .catch((resaon) => {
                    //         meshLog(resaon);
                    //     })
                }
            }
        }
        const id = setInterval(function () {
            //myFunction()  //获取版本信息用于同步，目前展示不用
        }, 30000)
        //30秒自动刷新的结束
        //停止自动刷新
        PubSub.unsubscribe("zidongdingshi")
        PubSub.subscribe('zidongdingshi', (msg, index) => {
            clearInterval(id)
            meshLog('bomb.jsx 177行 30秒自动刷新停止');
            PubSub.publish('setInterval', '')
            PubSub.publish('clearmqtt', '')
        })
    }
    render() {
        const flag = this.state.flag;
        if (flag == 1) {
            return <Navigate to="/admin/home" />//返回的到home
        } else {
            return (
                <div className='bobb' >
                    <div className='bombboby' style={{ position: 'fixed' }}  >
                    </div>
                    <div className='kuang' style={{ position: 'fixed' }} >
                        <div style={{ display: 'flex', height: 40, textAlign: 'center' }}>
                            <span style={{ width: '100%', fontSize: 25, color: "#FFFFFF", marginLeft: 10, marginTop: 10, fontFamily: Regular }}>Welcome to </span>
                        </div>
                        <div style={{ display: 'flex', height: 40, textAlign: 'center' }}>
                            <span style={{ width: '100%', fontSize: 25, color: "#FFFFFF", marginLeft: 10, fontFamily: Regular }}>HSE BLE</span>
                        </div>
                        <div style={{ display: 'flex', height: 30, textAlign: 'center' }}>
                            <span style={{ width: '100%', fontSize: 13, color: "#FFFFFF", opacity: 0.7, marginLeft: 10, marginTop: 8, fontFamily: Regular }}>From the HSE BLE web application </span>
                        </div>
                        <div style={{ display: 'flex', height: 30, textAlign: 'center' }}>
                            <span style={{ width: '100%', fontSize: 13, color: "#FFFFFF", opacity: 0.7, marginLeft: 10, marginTop: 8, fontFamily: Regular }}>you can turn on/off and dim your lights </span>
                        </div>
                        <div style={{ display: 'flex', height: 30, textAlign: 'center' }}>
                            <span style={{ width: '100%', fontSize: 13, color: "#FFFFFF", opacity: 0.7, marginLeft: 10, marginTop: 8, fontFamily: Regular }}>and see their configurations.</span>
                        </div>
                        <div style={{ display: this.state.zongkongzhi == 0 ? 'block' : 'none' }}>
                            <div className='okan' style={{ display: 'flex', textAlign: 'center', marginLeft: 130, marginTop: 40 }} >
                                <Link to={{ pathname: this.state.dizhilan }} style={{ color: "#FFFFFF", marginLeft: 13, marginTop: 4 }} onClick={() => { this.closeBtnClick() }}  > OK</Link>
                            </div>
                        </div>
                        <div style={{ display: this.state.zongkongzhi == 1 ? 'block' : 'none' }}>
                            <Spin style={{ marginLeft: 140, marginTop: 40 }} ></Spin>
                        </div>
                    </div>
                </div >
            );
        }
    }
};
export default Bomb
